import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.aliyun.odps.utils.StringUtils;

public class TestTitle {
	public static void main(String[] args) {
		testSplit();
		
		//String brand = "Phisoderm";
		//String aUrl = "https://www.ewg.org/skindeep/product/464459/Phisoderm_Tear-Free_Cream_Wash/";
		//String bUrl = "https://www.ewg.org/skindeep/product/599408/Phisoderm_Tear-Free_Cream_Wash_%282014_formulation%29/";
		//String aTitle = "Ahava Time To Smooth Age Control Even Tone Moisturizer, SPF 20 (2016 formulation)";
		//String bTitle = "Ahava Time to Smooth Age Control Even Tone Moisturizer, SPF 20 (2017 formulation)";
		//int hashCode2 = bTitle.hashCode();
		//int hashCode = aTitle.hashCode();
		//System.out.println(hashCode +"-----"+hashCode2);
		//byte[] bytes = aTitle.getBytes();
		//byte[] bytes2 = bTitle.getBytes();
		//System.out.println(bytes.length +"-----"+bytes2.length);
		
		//System.out.println(evaluate(brand, aUrl, aTitle, bUrl, bTitle));
		
		

	}

	public static Double evaluate(String brand, String aUrl, String aTitle, String bUrl, String bTitle) {
		try {
			// 同一商品不做比较
			if (StringUtils.equals(aUrl, bUrl)) {
				return null;
			}
			// 替换title中的brand
			aTitle = aTitle.replace(brand, "");
			bTitle = bTitle.replace(brand, "");
			System.out.println(aTitle + "----------" + bTitle);
			// 不同商品将title转成大写，除去空格，切成数组
			String[] aArr = aTitle.toUpperCase().replace(" ", "").split("");
			String[] bArr = bTitle.toUpperCase().replace(" ", "").split("");

			// 获取短数组的长度
			int minArrLength = aArr.length > bArr.length ? bArr.length : aArr.length;
			StringBuffer sb = new StringBuffer();

			// 循环
			for (int i = 0; i < minArrLength; i++) {
				if (!StringUtils.equals(aArr[i], bArr[i])) {
					break;
				}
				sb.append(aArr[i]);
			}

			// 相似部分
			String samePart = sb.toString();
			if (StringUtils.isBlank(samePart)) {
				// 为空 相似度为0
				return 0.00;
			} else {
				int sameLength = samePart.length();
				DecimalFormat decimalFormat = new DecimalFormat(".00");
				String format = decimalFormat.format(Double.valueOf(sameLength) / Double.valueOf(minArrLength));
				Double per = Double.valueOf(format);
				System.out.println(per + "---------");
				return per;
			}
		} catch (Exception e) {
			return 0.8;
		}

	}
	
	
	public static void testSplit() {
		String url = "https://static.ewg.org/skindeep/img/draw_score/score_image8_8_1_.png";
		//https://static.ewg.org/skindeep/img/draw_score/score_image1_1_1_.png; 1
		//https://static.ewg.org/skindeep/img/draw_score/score_image2_1_1_.png; 1-2
		//https://static.ewg.org/skindeep/img/draw_score/score_image8_8_1_.png; 8
		//https://static.ewg.org/skindeep/img/draw_score/score_image4_4_1_.png; 4
		//https://static.ewg.org/skindeep/img/draw_score/score_image3_1_1_.png; 1-3
		
		String substring = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
		//获取2_1   8_8那部分
		String partScore = substring.substring(11, 14);
		String[] split = partScore.split("_");
		//因为他的分1-1是1  2-1是1-2  8-8是8 所以要去重
		Set<Integer> set = new HashSet<Integer>();
		for (int i = 0; i < split.length; i++) {
			set.add(Integer.valueOf(split[i]));
		}
		StringBuffer sb = new StringBuffer();
		for (Integer score : set) {
			sb.append(score).append("-");
		}
		String score = (String) sb.subSequence(0, sb.lastIndexOf("-"));
		System.out.println(score);
	}

}
